package com.daxia.bumall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.bumall.common.UserType;
import com.daxia.bumall.dto.JsonResultDTO;
import com.daxia.bumall.dto.OrderDesignerDTO;
import com.daxia.bumall.dto.UserDTO;
import com.daxia.bumall.model.Company;
import com.daxia.bumall.model.Designer;
import com.daxia.bumall.model.User;
import com.daxia.bumall.service.OrderDesignerService;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/orderDesigner", produces="text/html;charset=UTF-8")
public class AdminOrderDesignerController extends BaseController {

	@Autowired
	private OrderDesignerService orderDesignerService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			OrderDesignerDTO dto = orderDesignerService.load(id);
			map.put("n", dto);
		}
		return "admin/orderDesigner/orderDesigner_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(OrderDesignerDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			orderDesignerService.create(dto);
		} else {
			orderDesignerService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		orderDesignerService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	public String list(OrderDesignerDTO dto, Map<String, Object> map, Page page) {
		UserDTO currentUser = getCurrentUserDTO();   //获取当前用户
		//根据用户类型来获取预约权限
		if(currentUser.getType() == UserType.City.getValue()) {   //城市运营商能看到本城市的所有用户的预约,即被预约用户城市=当前用户城市
			User cityUser = new User();
			cityUser.setCity(currentUser.getCity());
			Designer cityDesigner = new Designer();
			cityDesigner.setUser(cityUser);
			dto.setDesigner(cityDesigner);
		} else if(currentUser.getType() == UserType.DecorateCompany.getValue()) { //装修公司能看到下属设计师的预约，即被预约用户公司=当前账户（装修公司）id
			Company designCompany = new Company();
			designCompany.setUser(currentUser);
			Designer cityDesigner = new Designer();
			cityDesigner.setCompanyUser(designCompany);
			dto.setDesigner(cityDesigner);
		} else if(currentUser.getType() == UserType.Manager.getValue()) {  //管理员账户，不需要管理，只是为其他用户进行分离
			
		} else {   //其他用户，不管如何，都只能访问当前用户的预约
			Designer designer = new Designer();
			designer.setUser(currentUser);
		}
		
		List<OrderDesignerDTO> dtos = orderDesignerService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("companys", companyService.findAll());
		return "admin/orderDesigner/orderDesigner_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(OrderDesignerDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<OrderDesignerDTO> dtos = orderDesignerService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (OrderDesignerDTO orderDesigner : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", orderDesigner.getId());
	        jsonObject.put("user.nickName", orderDesigner.getUser().getNickName());    	
            jsonObject.put("communityName", orderDesigner.getCommunityName());
            jsonObject.put("area", orderDesigner.getArea());
			jsonObject.put("orderTime", formatDate(orderDesigner.getOrderTime(), "yyyy-MM-dd"));	            	
			jsonObject.put("createTime", formatDate(orderDesigner.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));	            	
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
}
